const {common: common} = require("../../../../common/common");

// leaderPerson/pages/groupGragon/index.js
Page({
    /**
   * 页面的初始数据
   */
    data: {
        key_word: "",
        type: 0,
        page: 1,
        filters: [ {
            value: 0,
            label: "全部"
        }, {
            value: 1,
            label: "接龙中"
        }, {
            value: 2,
            label: "未开始"
        }, {
            value: 3,
            label: "已结束"
        }, {
            value: 4,
            label: "审核中"
        }, {
            value: 5,
            label: "已失效"
        } ],
        lists: []
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad(options) {},
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow() {
        this.getList();
    },
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh() {
        var that = this;
        that.setData({
            page: 1,
            lists: []
        });
        that.getList();
        wx.stopPullDownRefresh();
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom() {
        this.getList();
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function(e) {
        console.log(e);
        if (e.target.dataset.type == "drangon") {
            return {
                title: e.target.dataset.item.name,
                imageUrl: e.target.dataset.item.img.length ? e.target.dataset.item.img[0] : "",
                path: `/dragon/pages/dragonDetail/dragonDetail?id=${e.target.dataset.item.id}`
            };
        }
    },
    /**
   * 切换筛选
   */
    changeFilter: function(e) {
        this.setData({
            type: e.currentTarget.dataset.value,
            page: 1
        });
        this.getList();
    },
    /**
   * 新增活动
   */
    create: function() {
        wx.navigateTo({
            url: "/leaderPerson/pages/groupDragon/Entrance/index"
        });
    },
    search: function() {
        this.setData({
            page: 1
        });
        this.getList();
    },
    recordValue: function(e) {
        var that = this;
        that.setData({
            key_word: e.detail.value || ""
        });
    },
    /**
   * 获取我的群接龙列表
   */
    getList: function() {
        let that = this;
        wx.showLoading({
            title: "加载中..."
        });
        common.request({
            endpoint: "Leader/leader-group/my-list",
            method: "post",
            data: {
                flag: that.data.type,
                page: that.data.page,
                key_word: that.data.key_word
            },
            success: res => {
                if (res.code == 0) {
                    let lists = res.extra;
                    lists = that.data.page == 1 ? lists : that.data.lists.concat(lists);
                    that.setData({
                        lists: lists,
                        page: lists.length ? that.data.page + 1 : 1
                    });
                    wx.hideLoading();
                } else {
                    wx.hideLoading();
                    wx.showModal({
                        title: "提示",
                        content: res.message,
                        showCancel: false,
                        complete: res => {
                            if (res.confirm) {}
                        }
                    });
                }
            }
        });
    },
    /**
   * 推荐/取消推荐
   * @param {*} e 
   */
    recommendOper: function(e) {
        let that = this;
        let id = e.detail.value;
        let flag = e.detail.flag;
        //0取消推荐，1推荐
                wx.showModal({
            title: "提示",
            content: `是否${flag == 0 ? "取消推荐" : "推荐"}？`,
            complete: res => {
                if (res.cancel) {
                    return false;
                }
                if (res.confirm) {
                    common.request({
                        endpoint: "Leader/leader-group/recommend",
                        method: "post",
                        data: {
                            id: id,
                            flag: flag
                        },
                        success: res => {
                            if (res.code == 0) {
                                wx.showToast({
                                    icon: "none",
                                    title: "操作成功！"
                                });
                                that.setData({
                                    page: 1
                                });
                                that.getList();
                            } else {
                                wx.showModal({
                                    title: "提示",
                                    content: res.message,
                                    showCancel: false,
                                    complete: res => {
                                        if (res.confirm) {}
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });
    },
    /**
   * 提前开始接龙活动
   * @param {*} e 
   */
    startDragon: function(e) {
        let that = this;
        let id = e.detail.value;
        wx.showModal({
            title: "提示",
            content: "是否提前开始该接龙？",
            complete: res => {
                if (res.cancel) {
                    return false;
                }
                if (res.confirm) {
                    common.request({
                        endpoint: "Leader/leader-group/early-start",
                        method: "post",
                        data: {
                            id: id
                        },
                        success: res => {
                            if (res.code == 0) {
                                wx.showToast({
                                    icon: "none",
                                    title: "操作成功！"
                                });
                                that.setData({
                                    page: 1,
                                    type: 0
                                });
                                that.getList();
                            } else {
                                wx.showModal({
                                    title: "提示",
                                    content: res.message,
                                    showCancel: false,
                                    complete: res => {
                                        if (res.confirm) {}
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });
    },
    /**
   * 提前结束接龙活动
   */
    endDragon: function(e) {
        let that = this;
        let id = e.detail.value;
        wx.showModal({
            title: "提示",
            content: "是否提前结束该接龙？",
            complete: res => {
                if (res.cancel) {
                    return false;
                }
                if (res.confirm) {
                    common.request({
                        endpoint: "Leader/leader-group/shot-down",
                        method: "post",
                        data: {
                            id: id
                        },
                        success: res => {
                            if (res.code == 0) {
                                wx.showToast({
                                    icon: "none",
                                    title: "操作成功！"
                                });
                                that.setData({
                                    page: 1,
                                    type: 0
                                });
                                that.getList();
                            } else {
                                wx.showModal({
                                    title: "提示",
                                    content: res.message,
                                    showCancel: false,
                                    complete: res => {
                                        if (res.confirm) {}
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });
    },
    /**
   * 编辑接龙活动
   * @param {*} e 
   */
    editDragon: function(e) {
        let id = e.detail.value;
        wx.navigateTo({
            url: `/leaderPerson/pages/groupDragon/Create/create?id=${id}`
        });
    },
    /**
   * 复制接龙活动
   * @param {*} e 
   */
    copyDragon: function(e) {
        let id = e.detail.value;
        wx.navigateTo({
            url: `/leaderPerson/pages/groupDragon/Create/create?copy_id=${id}&opetype=3`
        });
    }
});